<template>
  <div class="container">
    <el-card style="margin: 10px 10px">
      <!-- 按钮 -->
      <el-row type="flex" justify="end">
        <el-button
          class="el-icon-edit"
          style="margin-right: 60px; margin-bottom: 10px"
          size="small"
          type="success"
          @click="$router.push('/questions/new')"
          >新增试题</el-button
        >
      </el-row>
      <!-- 表单 -->
      <el-form :inline="true" label-width="80px" :model="form" ref="form">
        <el-row>
          <el-col :span="6">
            <el-form-item label="学科" prop="subjectID">
              <el-select
                placeholder="请选择"
                @change="changeSubject"
                v-model="form.subjectID"
                size="small"
                style="width: 223px"
              >
                <el-option
                  v-for="item in subjectList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="catalogID" label="二级目录">
              <el-select
                v-model="form.catalogID"
                placeholder="请选择"
                size="small"
                style="width: 223px"
              >
                <el-option
                  v-for="item in directoryList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签" prop="tags">
              <el-select
                placeholder="请选择"
                size="small"
                style="width: 223px"
                v-model="form.tags"
              >
                <el-option
                  v-for="item in TagsList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字" prop="keyword">
              <el-input
                style="width: 223px"
                size="small"
                v-model="form.keyword"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="试题类型" prop="questionType">
              <el-select
                placeholder="请选择"
                size="small"
                style="width: 223px"
                v-model="form.questionType"
              >
                <el-option
                  v-for="item in questionType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度" prop="difficulty">
              <el-select
                placeholder="请选择"
                size="small"
                style="width: 223px"
                v-model="form.difficulty"
              >
                <el-option
                  v-for="item in difficulty"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向" prop="direction">
              <el-select
                placeholder="请选择"
                size="small"
                style="width: 223px"
                v-model="form.direction"
              >
                <el-option
                  v-for="item in direction"
                  :key="item"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人" prop="creatorID">
              <el-select
                placeholder="请选择"
                size="small"
                style="width: 223px"
                v-model="form.creatorID"
              >
                <el-option
                  v-for="item in users"
                  :key="item.id"
                  :label="item.username"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="题目备注" prop="remarks">
              <el-input
                style="width: 223px"
                size="small"
                v-model="form.remarks"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称" prop="shortName">
              <el-input
                style="width: 223px"
                size="small"
                v-model="form.shortName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市" prop="city">
              <el-select
                placeholder="请选择"
                size="small"
                style="width: 100px; margin-right: 5px"
                v-model="form.city"
                @change="handlearea"
              >
                <el-option
                  v-for="item in provinces"
                  :key="item"
                  :label="item.city"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="province">
              <el-select
                placeholder="请选择"
                size="small"
                style="width: 100px"
                v-model="form.province"
              >
                <el-option
                  v-for="item in areas"
                  :key="item"
                  :label="item.city"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="margin-left: 185px">
              <el-button size="small" @click="cleanForm">清除</el-button>
              <el-button type="primary" size="small" @click="search">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 警告弹出框 -->
      <el-alert
        type="info"
        show-icon
        style="margin-bottom: 15px"
        :closable="false"
      >
        <template> 数据一共 {{ total }} 条 </template>
      </el-alert>
      <!-- 表格 -->
      <el-table :data="questionList" style="width: 100%">
        <el-table-column label="试题编号" prop="number"> </el-table-column>
        <el-table-column label="学科" width="80px" prop="subject">
        </el-table-column>
        <el-table-column label="目录" width="120px" prop="direction">
        </el-table-column>
        <el-table-column label="题型" width="80px">
          <template v-slot="{ row }">
            {{
              questionType.find((item) => item.value === +row.questionType)
                .label
            }}
          </template>
        </el-table-column>
        <el-table-column label="题干">
          <template v-slot="{ row }">
            <div v-html="row.question"></div>
          </template>
        </el-table-column>
        <el-table-column label="录入时间">
          <template v-slot="{ row }">
            {{ row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="难度" width="80px">
          <template v-slot="{ row }">
            {{
              difficulty.find((item) => item.value === +row.difficulty).label
            }}
          </template>
        </el-table-column>
        <el-table-column label="录入人" prop="creator"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template v-slot='{row}'>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-view"
              circle
              title="预览"
              @click="showDialogs(row)"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              circle
              title="修改"
              @click="$router.push(`new?id=${row.id}`)"
            ></el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              title="删除"
              circle
              @click="del(row.id)"
            ></el-button>
            <el-button
              type="success"
              size="small"
              icon="el-icon-check"
              circle
              title="加入精选"
              @click="choiceness(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="end" style="margin-top: 15px">
        <el-pagination
          :current-page="form.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="form.pagesize"
          layout="prev,pager,next,sizes, jumper"
          :total="total"
          @current-change="changePage"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <TopicPreView :showDialog.sync='showDialog' :preViewDatas='preViewDatas'></TopicPreView>
  </div>
</template>

<script>
// 基础题库列表
import { choiceAdd, list as questionList, remove } from '../../api/hmmm/questions'
// 简单学科列表
import { simple as subjectList } from '../../api/hmmm/subjects'
// 目录简单列表
import { simple as optionsList } from '../../api/hmmm/directorys'
// 标签简单列表
import { simple as getTags } from '../../api/hmmm/tags'
// 枚举数据
import { questionType, difficulty, direction } from '../../api/hmmm/constants'
// 获取录入人信息
import { simple as userPeople } from '../../api/base/users'
// 获取城市和省份
import { provinces, citys } from '../../api/hmmm/citys'
// 预览弹层组件
import TopicPreView from '../components/Topic preview.vue'
export default {
  components: {
    TopicPreView
  },
  data () {
    return {
      showDialog: false,
      // 基础题库的列表
      questionList: [],
      // 学科目录
      subjectList: [],
      // 二级目录
      directoryList: [],
      // 标签目录
      TagsList: [],
      form: {
        subjectID: null, // 学科
        catalogID: null, //
        tags: null, // 标签
        keyword: null, // 搜索关键字
        questionType: null, // 题型
        difficulty: null, // 难度
        direction: null, // 方向
        city: null, // 城市
        province: null, // 地区
        remarks: null, //
        shortName: null, //
        creatorID: null, //
        page: 1, // 当前页
        pagesize: 5 // 每页显示的条数
      },
      // 枚举数据题型
      questionType,
      // 难度
      difficulty,
      // 方向
      direction,
      // 录入人
      users: [],
      // 所有城市
      provinces: provinces(),
      // 所有地区
      areas: [],
      // 分页总数
      total: 0,
      // 传送id给组件使用
      preViewDatas: {}
    }
  },
  created () {
    // 获得题库列表
    this.getQuestionList()
    // 获得学科列表
    this.getSubjectList()
    // 获得录入人
    this.getusers()
  },
  methods: {
    // 获得录入人
    async getusers () {
      const res = await userPeople()
      // console.log(res)
      this.users = res.data
    },
    // 获得基础题库的列表
    async getQuestionList () {
      const res = await questionList(this.form)
      // console.log(res.data.items)
      // console.log(res)
      this.total = res.data.counts
      this.questionList = res.data.items
    },
    // 获得学科简单列表
    async getSubjectList () {
      const res = await subjectList()
      // console.log(res)
      this.subjectList = res.data
      // console.log(this.subjectList)
    },
    // 学科二级目录获取
    async changeSubject () {
      // console.log(this.form.subjectID)
      if (this.form.subjectID) {
        // 根据学科id 获得标签和二级目录的列表
        const { data: directoryList } = await optionsList({
          subjectID: this.form.subjectID
        })
        // console.log(res)
        this.directoryList = directoryList
        const { data: TagsList } = await getTags({
          subjectID: this.form.subjectID
        })
        // console.log(res)
        this.TagsList = TagsList
      } else {
        this.directoryList = null
        this.TagsList = null
      }
    },
    // 地区处理
    handlearea () {
      // 获取地区列表
      this.areas = null
      const res = citys(this.form.city)
      // console.log(res)
      this.areas = res
    },
    // 清空表单 这边注意.resetFields()方法只能清空value的值，如果有select框的话，需要给每个el-form-item 加上prop属性，
    // 才可以生效
    cleanForm () {
      this.$refs.form.resetFields()
      this.areas = null
    },
    // 处理点击切换
    changePage (page) {
      // console.log(this.form.page)
      // 这里注意更改页码之后要重新渲染
      this.form.page = page
      this.getQuestionList()
    },
    // 每页显示数据
    handleSizeChange (pagesize) {
      // console.log(pagesize)
      this.form.pagesize = pagesize
      this.getQuestionList()
    },
    // 搜索筛选功能
    search () {
      this.form.page = 1
      this.getQuestionList()
    },
    // 删除功能的完成
    async del (id) {
      // console.log(id)
      await this.$confirm('您确定要删除吗', '温馨提示', { type: 'warning' })
      await remove({ id })
      // console.log(res)
      if (this.questionList.length === 1) {
        this.form.page--
      }
      this.$message.success('删除成功')
      this.getQuestionList()
    },
    // 加入精选功能的完成
    async choiceness (id) {
      // console.log(id)
      await this.$confirm('您确定要加入精选吗', '温馨提示', { type: 'warning' })
      await choiceAdd({
        id,
        choiceState: 1
      })
      this.$message.success('添加成功')
      this.getQuestionList()
    },
    // 预览数据的存储
    showDialogs (row) {
      this.showDialog = true
      this.preViewDatas = row
      // console.log(this.preViewData)
    }
  }
}
</script>

<style scoped lang='scss'>
</style>
